import _ from 'lodash';
import moment from 'moment';
import { message } from 'antd';
import type { IOrginDealElementOptions } from './type';

// 处理交易单元数据
export const getDealElementOptions = (data: IOrginDealElementOptions[]) => {
  const _settlemen: Record<string, any> = {};
  let dataObj: Record<string, any> = {};
  const modalData: any[] = data.map((m: IOrginDealElementOptions) => {
    const _children = m?.children?.map((item: IOrginDealElementOptions) => {
      const children = item?.children?.map((n) => ({
        ...n,
        title: n?.label,
        key: n?.value,
        parentKey: item?.id,
        regionId: m?.value || '',
      }));
      dataObj = { ...dataObj, ..._.groupBy(children, 'value') };
      _settlemen[item?.id ? item?.id : ''] = children;
      return { ...item, key: item?.id, title: item?.label || '', children, parentKey: m?.id };
    });

    return { ...m, title: m?.label || '', children: _children, key: m?.value || '' };
  });
  return { modalData, value: _.flatten(Object.values(_settlemen)), dataObj };
};

// 数据保留小数位
export const ToFixed = (data: any, persion = 3) => {
  if (!data) return data;
  const dot = String(data).indexOf('.');
  let num = Number(data);
  if (dot !== -1) {
    const dotCnt = String(data).substring(dot + 1, data.length);
    if (dotCnt.length > persion) {
      num = Number(Number(data).toFixed(persion));
    } else {
      num = Number(data);
    }
  }
  return num;
};

export const TIME_ENUM = (point: 96 | 24) => {
  const timeArr = new Array(point);
  const time = moment().startOf('date');
  const pointArray: string[] = [];
  if (point === 96) {
    _.forEach(timeArr, () => {
      time.add(15, 'minutes');
      const timeData = `${time.format('HHmm') === '0000' ? '2400' : time.format('HHmm')}`;
      pointArray.push(timeData);
    });
  } else {
    _.forEach(timeArr, () => {
      time.add(60, 'minutes');
      const timeData = `${time.format('HHmm') === '0000' ? '2400' : time.format('HHmm')}`;
      pointArray.push(timeData);
    });
  }
  return pointArray;
};
